﻿var eventID;
var modeJsonObj = [];
var bedNo;
var visitID;
var tempPatientID = "";
var tempPatientName = "";
var firsLoad = true;
$(function () {
    eventID = $("#hdnEventID").val();

    var eventTypeData;
    $("#eventTypeID").ligerComboBox({
        width: 140,
        cancelable: false,
        url: "/AdverseEvent/EditAdverseEvent/GetAdverseEventTypeList?ram" + Math.random(),
        valueField: "ID",
        textField: "Name",
        valueFieldID: 'hdnEventTypeID',
        disabled: true,
        onSelected: function (value) {
            displayMode();
            for (var i = 0; i < eventTypeData.length; i++) {
                if (eventTypeData[i].ID == value) {
                    $("#eventName").text(eventTypeData[i].Name);
                    if (eventTypeData[i].Remark != null && eventTypeData[i].Remark != "") {
                        $("#eventRemark").text(eventTypeData[i].Remark);
                        $("#trEventRemark").show();
                    }
                    else {
                        $("#trEventRemark").hide();
                    }
                }
            }
        },
        onSuccess: function (data) {
            eventTypeData = data;
            if (eventID == 0) {
                $("#eventTypeID").ligerComboBox().setValue(6);
            }
        }
    });

    $("#areaID").ligerComboBox({
        cancelable: false,
        width: 140,
        url: "/AdverseEvent/EditAdverseEvent/GetAllUnits?ram" + Math.random(),
        valueField: "DepartmentCode",
        textField: "DepartmentName",
        valueFieldID: 'hdnAreaID',
        onSelected: function (value) {
            getPatientList();
        },
        onSuccess: function (data) {
            if (eventID == 0) {
                $("#areaID").ligerComboBox().setValue($("#hdnCurrDepCode").val());
            }
        }
    });

    $("#patientID").ligerComboBox({
        cancelable: false,
        autocomplete: true,
        keySupport: true,
        width: 140,
        valueField: "PATIENT_ID",
        textField: "PATIENT_NAME",
        valueFieldID: 'hdnPatientID',
        onSelected: function (value) {
            if (tempPatientID == "") {
                var data = $("#patientID").ligerComboBox().data;
                for (var i = 0; i < data.length; i++) {
                    if (data[i].PATIENT_ID == value) {
                        $("#txtPatientID").val(value);
                        $("#txtPatientSex").val(data[i].SEX);
                        if (!isNaN(data[i].AGE)) {
                            $("#txtPatientAge").val(data[i].AGE);
                            $("#divAge").show();
                            $("#divAgeStr").hide();
                        }
                        else {
                            $("#txtPatientAgeStr").val(data[i].AGE);
                            $("#divAge").hide();
                            $("#divAgeStr").show();
                        }
                        $("#txtPatientInTime").val(data[i].IN_DATE);
                        $("#patientNursingGrade").ligerComboBox().setValue($("#patientNursingGrade").ligerComboBox().findValueByText(data[i].NURSING));
                        $("#txtPatientDiagnosis").val(data[i].DIAGNOSE);
                        bedNo = data[i].BED_NO;
                        visitID = data[i].VISIT_ID;
                        break;
                    }
                }
            }
            else {
                if ($("#patientID").ligerComboBox().getText() == "") {
                    $("#patientID").ligerComboBox().insertItem({ PATIENT_ID: tempPatientID, PATIENT_NAME: " " + tempPatientName }, 0);
                    $("#patientID").ligerComboBox().setValue(tempPatientID);
                    $("#patientID").ligerComboBox().setDisabled(false);
                }
            }
        }
    });

    $("#patientNursingGrade").ligerComboBox({
        cancelable: false,
        width: 140,
        data: [
            { text: '全部', id: 'c001' },
            { text: '新入', id: 'c002' },
            { text: '一般', id: 'c003' },
            { text: '病危', id: 'c004' },
            { text: '病重', id: 'c005' },
            { text: '特级护理', id: 'c006' },
            { text: 'Ⅰ级护理', id: 'c007' },
            { text: 'Ⅱ级护理', id: 'c008' },
            { text: '重症监护', id: 'c009' },
            { text: '今日手术', id: 'c0010' },
            { text: '术后1天', id: 'c0011' },
            { text: '术后2天', id: 'c0012' },
            { text: '术后3天', id: 'c0013' },
            { text: '明日出院', id: 'c0014' }
        ],
        valueField: "id",
        textField: "text",
        valueFieldID: 'hdnPatientNursingGrade',
        onSelected: function (value) {
        }
    });
    $("#trNursingGrade").hide();

    $("#txtPatientInTime,#txtTransferTime").ligerDateEditor({
        showTime: true,
        format: "yyyy-MM-dd hh:mm:ss",
        width: 140,
        cancelable: false
    });

    $("#isDischargedPatients").click(function () {
        $("#patientID").ligerComboBox().setDisabled();
        getPatientList();
    });

    $("#btnBack").click(function () {
        if ($("#hdnPageType").val() == 0) {
            location.href = "/AdverseEvent/PressureSoreEventList";
        }
        else {
            location.href = "/AdverseEvent/AdverseEventReview";
        }
    });

    $("#btnReport").click(function () {
        location.href = "/AdverseEvent/EditPressureSoreEvent/Report?eventID=" + eventID;
    });

    $(".trFanKui").dblclick(function () {
        ycFanKui();
    });

    bindAdverseEvent();

    getYcFanKui();

    $("#pageloading").hide();
});

function getPatientList() {
    $.ajax({
        url: "/AdverseEvent/EditAdverseEvent/GetPatientList?ram" + Math.random(),
        type: "post",
        data: { departmentCode: $("#hdnAreaID").val(), isDischargedPatients: ($("#isDischargedPatients").is(':checked') ? 1 : 0) },
        success: function (r) {
            var d = $.parseJSON(r);
            for (var i = 0; i < d.length; i++) {
                d[i].PATIENT_NAME = d[i].BED_NO + " " + d[i].PATIENT_NAME;
            }
            $("#patientID").ligerComboBox({ data: d, dataList: d }).setEnabled();
            $("#patientID").ligerComboBox().updateStyle();
            if (eventID == 0) {
                if (d.length > 0)
                    $("#patientID").ligerComboBox().setValue(d[0].PATIENT_ID);
            }
            else {
                if (tempPatientID != "") {
                    $("#patientID").ligerComboBox().setValue(tempPatientID);
                    tempPatientID = "";
                }
            }
        }
    });
}

function bindAdverseEvent() {
    if (eventID != 0) {
        $.ajax({
            url: "/AdverseEvent/EditAdverseEvent/GetAdverseEvent?ram" + Math.random(),
            type: "post",
            data: { id: eventID },
            success: function (r) {
                var event = $.parseJSON(r);
                if (event.Status != $("#hdnStatus").val()) {
                    $("#pageloading").hide();
                    $.ligerDialog.error("事件状态已改变！");
                    setTimeout(function () { $("#btnBack").click(); }, 1000);
                    return;
                }
                $("#eventTypeID").ligerComboBox().setValue(event.EventTypeID);
                $("#areaID").ligerComboBox().setValue(event.OccurrDepartID);
                var _PatientID = event.PatientID;
                if (_PatientID != "" && _PatientID != null)
                    tempPatientID = event.PatientID.replace(/\s+/g, "");
                tempPatientName = event.PatientName;
                $("#txtPatientID").val(event.PatientID);
                $("#txtPatientSex").val(event.PatientSex);
                $("#txtPatientAge").val(event.PatientAge == 0 ? "" : event.PatientAge);
                $("#txtPatientAgeMonth").val(event.PatientAgeMonth == 0 ? "" : event.PatientAgeMonth);
                $("#txtPatientAgeWeek").val(event.PatientAgeWeek == 0 ? "" : event.PatientAgeWeek);
                $("#txtPatientAgeDay").val(event.PatientAgeDay == 0 ? "" : event.PatientAgeDay);
                $("#txtPatientAgeStr").val(event.PatientAgeStr);
                if (!IsEmpty(event.PatientAgeStr)) {
                    $("#divAge").hide();
                    $("#divAgeStr").show();
                }
                else {
                    $("#divAge").show();
                    $("#divAgeStr").hide();
                }
                $("#txtPatientInTime").val(event.PatientInTime);
                $("#transferDepartCode").val(event.TransferDepartCode);
                $('input:radio[name="isTransfer"][value=' + event.IsTransfer + ']').attr('checked', 'true').click();
                $("#txtTransferTime").val(event.TransferTime);
                $("#txtPatientDiagnosis").val(event.PatientDiagnosis);
                if (event.ID > 0) {
                    $("#trCreateTime").show();
                    $("#txtCreateTime").val(event.CreateTime);
                }
                $("#patientNursingGrade").ligerComboBox().setValue(event.PatientNursingGrade);
                bedNo = event.PatientBedNo;
                visitID = event.PatientVisitID;
                $("#txtSkinCondition").val(event.SkinCondition);
                $("#txtExceptionalCase").val(event.ExceptionalCase);
                $("#txtDuDaoRemark").val(event.DuDaoRemark);
                $("#txtYuHou").val(event.YuHou);
                $("#txtZhuanGui").val(event.ZhuanGui);

                $("#btnReport").show();

                if ($("#hdnPageType").val() == 0) {
                    if (event.Status == 0 || event.Status == 3) {
                        $("#btn1").val("保存").show().click(function () { save(event.Status) });
                        $("#btn2").val("上报").show().click(function () { save(1) });
                    }
                }
                else {
                    var detailsViewBtns = JSON.parse($("#hdnDetailsViewBtns").val().replace(/([^\\])'/g, '$1\"'));
                    for (var i = 0; i < detailsViewBtns.length; i++) {
                        var detailsViewBtn = detailsViewBtns[i];
                        var run = detailsViewBtn.Function + '(' + detailsViewBtn.Parameter + ')';
                        $("#btn" + (i + 1)).val(detailsViewBtn.BtnName).attr("run", run).show().click(function () { eval($(this).attr("run")) });
                    }
                }

                if ($("#hdnStatus").val() >= 4 || ($("#hdnPageType").val() == 1 && $("#hdnStatus").val() == 2)) {
                    $(".trDuDaoJiLu").show();
                }
                else {
                    $(".trDuDaoJiLu").hide();
                }
            }
        });
    }
    else {
        $("#btn1").val("保存").show().click(function () { save(0) });
        $("#btn2").val("上报").show().click(function () { save(1) });
    }
}

function getYcFanKui() {
    $.ajax({
        url: "/AdverseEvent/EditPressureSoreEvent/GetYcFanKui?ram" + Math.random(),
        type: "post",
        data: { eventID: eventID },
        success: function (r) {
            $("#hdnYcFanKuiJson").val(r);
        }
    });
}

function displayMode() {
    $("[id^=tr_mode_]").each(function () {
        $(this).remove();
    })

    modeJsonObj = [];
    var typeID = $("#hdnEventTypeID").val();

    $.ajax({
        url: "/AdverseEvent/EditAdverseEvent/GetAdverseEventModeList?ram" + Math.random(),
        type: "post",
        async: false,
        data: { typeID: typeID, eventID: eventID },
        success: function (r) {
            modeJsonObj = $.parseJSON(r);
        }
    });

    for (var i = modeJsonObj.length - 1; i >= 0 ; i--) {
        var id = modeJsonObj[i].ModeID;
        var strTr = '<tr id="{0}" style="height: 35px;"><td style="width: 100px; text-align: right;vertical-align: text-top; padding-top: 8px;">{1}：</td><td style="line-height:35px;">{2}</td></tr>';
        strTr = strTr.replace("{0}", "tr_mode_" + id);
        if (modeJsonObj[i].ModeName.length > 10) {
            var modeName = modeJsonObj[i].ModeName.substring(0, 7) + "&nbsp;&nbsp;</br>";
            modeName += modeJsonObj[i].ModeName.substring(7, modeJsonObj[i].ModeName.length);
            strTr = strTr.replace("{1}", modeName);
        }
        else {
            strTr = strTr.replace("{1}", modeJsonObj[i].ModeName);
        }

        var isyclx = false;
        if (modeJsonObj[i].ModeName == "压伤类型") {
            isyclx = true;
        }

        if (!(modeJsonObj[i].ModeName == "压伤类型" || modeJsonObj[i].ModeName == "Braden评分分值" || modeJsonObj[i].ModeName == "感觉" || modeJsonObj[i].ModeName == "潮湿" || modeJsonObj[i].ModeName == "活动能力" || modeJsonObj[i].ModeName == "移动能力" || modeJsonObj[i].ModeName == "营养" || modeJsonObj[i].ModeName == "摩擦力")) {
            continue;
        }
        var isDate = false;
        var dateId = "";
        switch (modeJsonObj[i].InputType) {
            case 0:
                var temp = "";
                var values = [];
                if (modeJsonObj[i].Value != "" && modeJsonObj[i].Value != null) values = modeJsonObj[i].Value.split('#');
                var options = modeJsonObj[i].Options.split('#');
                for (var ii = 0; ii < options.length; ii++) {
                    var value = values[ii] == undefined ? "" : values[ii].split("$0%")[0];
                    var option = options[ii].split("$0%")[0];
                    temp += "<div style=\"float:left; margin-right:5px;\"><label for=\"mode_" + id + "_" + ii + "\" style=\"display:" + ((isyclx && ii == 1) ? "none;" : "") + "\">";
                    temp += "<input type=\"radio\" id=\"mode_" + id + "_" + ii + "\" name=\"mode_" + id + "\"" + (value == "1" ? "checked" : "") + " hdnValue=\"" + values[ii] + "\" hdnOption=\"" + options[ii] + "\"/><font>" + option + "</font></label></div>";
                    temp += "<div id=\"divDetailsMode_" + id + "_" + ii + "\" style=\"float:left; margin-right:5px;\"></div>";
                }
                strTr = strTr.replace("{2}", temp);
                break;
            case 1:
                var temp = "";
                var values = [];
                if (modeJsonObj[i].Value != "" && modeJsonObj[i].Value != null) values = modeJsonObj[i].Value.split('#');
                var options = modeJsonObj[i].Options.split('#');
                for (var ii = 0; ii < options.length; ii++) {
                    var value = values[ii] == undefined ? "" : values[ii].split("$0%")[0];
                    var option = options[ii].split("$0%")[0];
                    temp += "<div style=\"float:left; margin-right:5px;\"><label for=\"mode_" + id + "_" + ii + "\" style=\"display:" + ((isyclx && ii == 1) ? "none;" : "") + "\">";
                    temp += "<input type=\"checkbox\" id=\"mode_" + id + "_" + ii + "\" name=\"mode_" + id + "\"" + (value == "1" ? "checked" : "") + " hdnValue=\"" + values[ii] + "\" hdnOption=\"" + options[ii] + "\"/><font>" + option + "</font></label></div>";
                    temp += "<div id=\"divDetailsMode_" + id + "_" + ii + "\" style=\"float:left; margin-right:5px;\"></div>";
                }
                strTr = strTr.replace("{2}", temp);
                break;
            case 2:
                var temp = "<input type=\"text\" id=\"mode_" + id + "\" value=\"" + (modeJsonObj[i].Value == null ? "" : modeJsonObj[i].Value) + "\"  style=\"width: 440px;\" class=\"l-text\" maxlength=\"500\"/>";
                strTr = strTr.replace("{2}", temp);
                break;
            case 3:
                isDate = true;
                dateId = "mode_" + id;
                var temp = "<input type=\"text\" id=\"mode_" + id + "\" value=\"" + (modeJsonObj[i].Value == null ? "" : modeJsonObj[i].Value) + "\"/>";
                strTr = strTr.replace("{2}", temp);
                break;

        }
        $("#insertMode").after($(strTr));
        if (isDate) {
            setDateEditor(dateId);
        }
    }

    displayDetailsMode();
    $('input[name^=mode_]:checked').click().click();

    setYcBraden();
}

function displayDetailsMode() {
    $("input[name^=mode_]").click(function (item) {
        var eventTypeName = $("#eventTypeID").ligerComboBox().getText();
        if (eventTypeName == "压力性损伤") {
            var td = $(this).parent().parent().parent().prev();
            if (td.text() == "感觉：" || td.text() == "潮湿：" || td.text() == "活动能力：" || td.text() == "移动能力：" || td.text() == "营养：" || td.text() == "摩擦力：") {
                setYcBraden();
                return;
            }
        }
        if ($(this).is(":checked")) {
            if ($(this).is(":radio")) {
                $("[id^=" + $(this).attr("name").replace("mode", "divDetailsMode") + "_]").hide();
            }
            $(this).parent().parent().next().show();
            var hdnOption = $(this).attr("hdnOption");
            var hdnValue = $(this).attr("hdnValue");
            if (hdnValue == undefined || hdnValue == "undefined") hdnValue = "";
            if (hdnOption.indexOf("$0%0*") >= 0) {
                $(this).attr("inputType", 0);
                var vlist = hdnValue.split("$0%0*");
                var olist = hdnOption.split("$0%0*");
                if (olist.length > 1) {
                    var values = [];
                    if (vlist[1] != undefined) {
                        values = vlist[1].split('@');
                    }
                    var options = olist[1].split('@');
                    var id = $(this).attr("id");
                    var html = "";
                    for (var i = 0; i < options.length; i++) {
                        var value = values[i];
                        var option = options[i];
                        html += "<div style=\"float:left; margin-right:5px;\"><label for=\"" + id + "_" + i + "\">"
                        html += "<input type=\"radio\" id=\"details_" + id + "_" + i + "\" name=\"" + id + "\"" + (value == "1" ? "checked" : "") + "/>" + option + "</label></div>";
                    }
                    $(this).parent().parent().next().html(html);
                }
            }
            else if (hdnOption.indexOf("$0%1*") >= 0) {
                $(this).attr("inputType", 1);
                var vlist = hdnValue.split("$0%1*");
                var olist = hdnOption.split("$0%1*");
                if (olist.length > 1) {
                    var values = [];
                    if (vlist[1] != undefined) {
                        values = vlist[1].split('@');
                    }
                    var options = olist[1].split('@');
                    var id = $(this).attr("id");
                    var html = "";
                    for (var i = 0; i < options.length; i++) {
                        var value = values[i];
                        var option = options[i];
                        html += "<div style=\"float:left; margin-right:5px;\"><label for=\"" + id + "_" + i + "\">"
                        html += "<input type=\"checkbox\" id=\"details_" + id + "_" + i + "\" name=\"" + id + "\"" + (value == "1" ? "checked" : "") + "/>" + option + "</label></div>";
                    }
                    $(this).parent().parent().next().html(html);
                }
            }
            else if (hdnOption.indexOf("$0%2*") >= 0) {
                $(this).attr("inputType", 2);
                var id = $(this).attr("id");
                var vlist = hdnValue.split("$0%2*");
                var olist = hdnOption.split("$0%2*");
                if (olist.length > 1) {
                    var html = "<input type=\"text\" id=\"details_" + id + "\" value=\"" + (vlist[1] == null ? "" : vlist[1]) + "\"  style=\"width: 150px;\" class=\"l-text\" maxlength=\"500\"/>";
                    $(this).parent().parent().next().html(html);

                    var tk = $(this).next();
                    if (tk.text() == "他科带来压伤") {
                        $("#details_" + id).attr("id", "ddl_details_" + id);
                        bindDepartment("ddl_details_" + id);
                    }
                }
            }
            else if (hdnOption.indexOf("$0%3*") >= 0) {
                $(this).attr("inputType", 3);
                var id = $(this).attr("id");
                var vlist = hdnValue.split("$0%3*");
                var olist = hdnOption.split("$0%3*");
                if (olist.length > 1) {
                    var html = "<input type=\"text\" id=\"details_" + id + "\" value=\"" + (vlist[1] == null ? "" : vlist[1]) + "\"  style=\"width: 440px;\" class=\"l-text\" maxlength=\"500\"/>";
                    $(this).parent().parent().next().html(html);
                    setDateEditor(dateId);
                }
            }
        }
        else {
            $(this).parent().parent().next().hide();
        }
    });
}

function bindDepartment(id) {
    $("#" + id).ligerComboBox({
        width: 120,
        cancelable: false,
        url: "/AdverseEvent/EditPressureSoreEvent/GetAllUnits?ram" + Math.random(),
        valueField: "DepartmentCode",
        textField: "DepartmentName",
        valueFieldID: id.replace(/ddl_/g, ''),
        value: $("#" + id).val(),
        css: "comboBoxMargin",
        onSuccess: function (data) {
            if ($("#" + id).val() == "") {
                $("#" + id).ligerComboBox().setValue(data[0].DepartmentCode);
            }
        }
    });
}

function setYcBraden() {
    var value = 0;
    var braden = null;
    var eventTypeName = $("#eventTypeID").ligerComboBox().getText();
    if (eventTypeName != "压力性损伤") return;
    $("#insertMode").nextAll().each(function () {
        var text = $(this).find('td').eq(0).text();
        if (text.indexOf("Braden") >= 0) {
            braden = $(this).find('td').eq(1).children().eq(0);
        }
        if (text == "感觉：" || text == "潮湿：" || text == "活动能力：" || text == "移动能力：" || text == "营养：" || text == "摩擦力：") {
            $(this).find('td').eq(1).find(":radio").each(function (i) {
                var label = $(this).parent();
                labelTexts = $(this).attr("hdnoption").split('☆');
                label.children().eq(1).text(labelTexts[0]);

                if ($(this).is(":checked")) {
                    value += parseInt(labelTexts[2]);
                }
            });
        }
    });
    if (braden != null) {
        if (value == 0) {
            braden.val("");
        }
        else {
            braden.val(value);
        }
        braden.attr("readonly", true);
    }
}

function save(status, isReview) {
    var EventTypeID = $("#hdnEventTypeID").val();
    var EventTypeName = $("#eventTypeID").ligerComboBox().getText();
    var AreaID = $("#hdnAreaID").val();
    var AreaName = $("#areaID").ligerComboBox().getText();
    var PatientID = $("#hdnPatientID").val();
    var PatientName = $("#patientID").ligerComboBox().getText().split(' ')[1];
    var PatientSex = $("#txtPatientSex").val();
    var PatientAge = $("#txtPatientAge").val();
    var PatientAgeMonth = $("#txtPatientAgeMonth").val();
    var PatientAgeWeek = $("#txtPatientAgeWeek").val();
    var PatientAgeDay = $("#txtPatientAgeDay").val();
    var PatientAgeStr = $("#txtPatientAgeStr").val();
    if ($("#divAge").is(":visible")) {
        PatientAgeStr = "";
    }
    else {
        var PatientAge = "";
        var PatientAgeMonth = "";
        var PatientAgeWeek = "";
        var PatientAgeDay = "";
    }
    var PatientInTime = $("#txtPatientInTime").val();
    var IsTransfer = $('input:radio[name="isTransfer"]:checked').val();
    var TransferTime = "";
    var TransferDepartCode = "";
    var TransferDepartName = "";
    if (IsTransfer == 1) {
        TransferTime = $("#txtTransferTime").val();
        TransferDepartCode = $("#hdnTransferDepartCode").val();
        TransferDepartName = $("#transferDepartCode").ligerComboBox().getText();
    }
    var PatientNursingGrade = $("#hdnPatientNursingGrade").val();
    var PatientDiagnosis = $("#txtPatientDiagnosis").val();
    var SkinCondition = $("#txtSkinCondition").val();
    var ExceptionalCase = $("#txtExceptionalCase").val();
    var YuHou = $("#txtYuHou").val();
    var ZhuanGui = $("#txtZhuanGui").val();
    if (IsEmpty(PatientName)) {
        $.ligerDialog.error("患者不能为空");
        return;
    }

    var ModeList = [];
    for (var i = 0; i < modeJsonObj.length; i++) {
        modeJsonObj[i].Value = "";
        if (modeJsonObj[i].InputType == 0 || modeJsonObj[i].InputType == 1) {
            $("[id^=mode_" + modeJsonObj[i].ModeID + "_]").each(function () {
                modeJsonObj[i].Value += "#" + ($(this).is(':checked') ? "1" : "0");

                if ($(this).is(':checked') && $(this).attr("inputType") != undefined) {
                    if ($(this).attr("inputType") == 0 || $(this).attr("inputType") == 1) {
                        if ($("[id^=details_" + $(this).attr("id") + "_]").length > 0) {
                            if ($(this).attr("inputType") == 0) {
                                modeJsonObj[i].Value += "$0%0*";
                            }
                            else {
                                modeJsonObj[i].Value += "$0%1*";
                            }
                            $("[id^=details_" + $(this).attr("id") + "_]").each(function () {
                                modeJsonObj[i].Value += ($(this).is(':checked') ? "1" : "0") + "@";
                            });
                            modeJsonObj[i].Value = modeJsonObj[i].Value.substring(0, modeJsonObj[i].Value.length - 1);
                        }
                    }
                    else {
                        if ($(this).attr("inputType") == 2) {
                            modeJsonObj[i].Value += "$0%2*";

                        }
                        if ($(this).attr("inputType") == 3) {
                            modeJsonObj[i].Value += "$0%3*";
                        }

                        modeJsonObj[i].Value += $("#details_" + $(this).attr("id")).val();
                    }
                }
            });
            modeJsonObj[i].Value = modeJsonObj[i].Value.substring(1, modeJsonObj[i].Value.length);
            if (modeJsonObj[i].Value.indexOf("1") > -1) {
                ModeList.push(modeJsonObj[i]);
            }
        }
        else {
            modeJsonObj[i].Value = $("#mode_" + modeJsonObj[i].ModeID).val();
            if (modeJsonObj[i].Value != "") {
                ModeList.push(modeJsonObj[i]);
            }
        }
    }

    var YcFanKui = $.parseJSON($("#hdnYcFanKuiJson").val());

    var AdverseEvent = {
        ID: eventID,
        EventTypeID: EventTypeID,
        EventTypeName: EventTypeName,
        OccurrDepartID: AreaID,
        OccurrDepartName: AreaName,
        PatientID: PatientID,
        PatientName: PatientName,
        PatientSex: PatientSex,
        PatientAge: PatientAge,
        PatientAgeMonth: PatientAgeMonth,
        PatientAgeWeek: PatientAgeWeek,
        PatientAgeDay: PatientAgeDay,
        PatientAgeStr: PatientAgeStr,
        PatientInTime: PatientInTime,
        PatientBedNo: bedNo,
        PatientVisitID: visitID,
        IsTransfer: IsTransfer,
        TransferTime: TransferTime,
        TransferDepartCode: TransferDepartCode,
        TransferDepartName: TransferDepartName,
        PatientNursingGrade: PatientNursingGrade,
        PatientDiagnosis: PatientDiagnosis,
        SkinCondition: SkinCondition,
        ExceptionalCase: ExceptionalCase,
        YuHou: YuHou,
        ZhuanGui: ZhuanGui,
        Status: status
    }
    var dataJson = {
        AdverseEvent: AdverseEvent,
        ModeList: ModeList,
        YcFanKui: YcFanKui,
        OldStatus: $("#hdnStatus").val()
    }

    if (isReview == 1) {
        return dataJson;
    }

    $.ajax({
        url: "/AdverseEvent/EditPressureSoreEvent/Save",
        type: 'post',
        data: JSON.stringify(dataJson),
        contentType: 'application/json;charset=utf-8',
        success: function (data) {
            var d = $.parseJSON(data);
            if (d.Result == 1) {
                eventID = d.Body;
                $("#hdnEventID").val(d.Body);
                if (status == 1 || status == 2) {
                    $.ligerDialog.success("上报成功");
                    setTimeout(function () { $("#btnBack").click(); }, 1000);
                }
                else {
                    $.ligerDialog.success("保存成功")
                }
                $("#btnReport").show();
            }
            else if (d.Result == 0) {
                $.ligerDialog.error(d.Body);
            }
            else {
                $.ligerDialog.error("程序错误");
            }
        },
        error: function () {
            $.ligerDialog.error("程序错误");
        }
    });
}

function ycFanKui() {
    $.ligerDialog.open({
        height: 330,
        width: 555,
        title: '编辑反馈信息',
        url: '/AdverseEvent/EditPressureSoreEvent/YcFanKui?eventId=' + eventID,
        showMax: false,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: { oldStatus: $("#hdnStatus").val() }
    });
}

function review(status) {
    $.ligerDialog.open({
        height: 300,
        width: 400,
        title: '请填写审核理由',
        url: '/AdverseEvent/AdverseEventReview/Review',
        showMax: false,
        showToggle: false,
        showMin: false,
        isResize: false,
        slide: false,
        data: { id: eventID, status: status, oldStatus: $("#hdnStatus").val(), duDaoRemark: $("#txtDuDaoRemark").val(), yuHou: $("#txtYuHou").val(), zhuanGui: $("#txtZhuanGui").val() },
        buttons: [{
            text: '回调函数', onclick: function (i, d) {
                $("#btnBack").click();
            }

        }]
    });
    $(".l-dialog-buttons").hide();
}

function bindTransferDepartment() {
    if (!IsEmpty($("#hdnTransferDepartCode").val())) return;
    $("#transferDepartCode").ligerComboBox({
        width: 120,
        cancelable: false,
        url: "/AdverseEvent/EditAdverseEvent/GetAllUnits?ram" + Math.random(),
        valueField: "DepartmentCode",
        textField: "DepartmentName",
        valueFieldID: "hdnTransferDepartCode",
        value: $("#transferDepartCode").val(),
        onSuccess: function (data) {
            if ($("#transferDepartCode").val() == "") {
                $("#transferDepartCode").ligerComboBox().setValue(data[0].DepartmentCode);
            }
        }

    });
}